import java.util.Arrays;

public class MyStack {
    public int[] elem;
    public int usedSize;
    public MyStack(){
        this.elem = new int[10];
    }
    //压栈
    public void push(int val){
        if(isFull()){
            //扩容
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize++] = val;//后置++,先赋值，后运算
    }
    public boolean isFull(){//判断栈是否已满
        return usedSize == elem.length;
    }
    //出栈
    public int pop(){
        if(isEmpty()){
            throw new EmptyException("栈是空的!");
        }
        int val = elem[--usedSize];//因为usedSize记录的是元素的个数，先减1，得到数组的最后一个元素的下标
        return val;
    }
    public int peek(){
        if(isEmpty()){
            throw new EmptyException("栈是空的！");
        }
        return elem[usedSize - 1];
    }
    public boolean isEmpty(){
        return usedSize == 0;
    }
}

